﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bymed.SpineScan.DataModel;
using Bymed.SpineScan.Service.Shared;
using Bymed.SpineScan.WPF.Model;
using Bymed.SpineScan.WPF.Shared;
using Bymed.SpineScan.WPF.ViewModel.TestItemViewModel;

namespace Bymed.SpineScan.WPF.ViewModel
{
    public class HistoryDataAnalysisViewModel : SingleTestHistoryDataViewModel
    {
        /// <summary>
        /// Initializes a new instance of the HistoryDataAnalysisViewModel class.
        /// </summary>
        public HistoryDataAnalysisViewModel(IDataService dataService)
            : base(dataService)
        {
            ResetProperties();
        }

        public override void ResetProperties()
        {
            base.ResetProperties();

            SetProperties(ServiceFactory.CreateBasicConfigurationService().Load());
        }

        /// <summary>
        /// The <see cref="CompanyName" /> property's name.
        /// </summary>
        public const string CompanyNamePropertyName = "CompanyName";

        private string _companyName = string.Empty;

        /// <summary>
        /// Sets and gets the CompanyName property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string CompanyName
        {
            get
            {
                return _companyName;
            }

            set
            {
                if (_companyName == value)
                {
                    return;
                }

                RaisePropertyChanging(CompanyNamePropertyName);
                _companyName = value;
                RaisePropertyChanged(CompanyNamePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="CompanyLogoFileFullName" /> property's name.
        /// </summary>
        public const string CompanyLogoFileFullNamePropertyName = "CompanyLogoFileFullName";

        private string _companyLogoFileFullName = string.Empty;

        /// <summary>
        /// Sets and gets the CompanyLogoFileFullName property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string CompanyLogoFileFullName
        {
            get
            {
                return _companyLogoFileFullName;
            }

            set
            {
                if (_companyLogoFileFullName == value)
                {
                    return;
                }

                RaisePropertyChanging(CompanyLogoFileFullNamePropertyName);
                _companyLogoFileFullName = value;
                RaisePropertyChanged(CompanyLogoFileFullNamePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="CompanyAddress" /> property's name.
        /// </summary>
        public const string CompanyAddressPropertyName = "CompanyAddress";

        private string _companyAddress = string.Empty;

        /// <summary>
        /// Sets and gets the CompanyAddress property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string CompanyAddress
        {
            get
            {
                return _companyAddress;
            }

            set
            {
                if (_companyAddress == value)
                {
                    return;
                }

                RaisePropertyChanging(CompanyAddressPropertyName);
                _companyAddress = value;
                RaisePropertyChanged(CompanyAddressPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="CompanyPhoneNumber" /> property's name.
        /// </summary>
        public const string CompanyPhoneNumberPropertyName = "CompanyPhoneNumber";

        private string _companyPhoneNumber = string.Empty;

        /// <summary>
        /// Sets and gets the CompanyPhoneNumber property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string CompanyPhoneNumber
        {
            get
            {
                return _companyPhoneNumber;
            }

            set
            {
                if (_companyPhoneNumber == value)
                {
                    return;
                }

                RaisePropertyChanging(CompanyPhoneNumberPropertyName);
                _companyPhoneNumber = value;
                RaisePropertyChanged(CompanyPhoneNumberPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="TestReportTitle" /> property's name.
        /// </summary>
        public const string TestReportTitlePropertyName = "TestReportTitle";

        private string _testReportTitle = string.Empty;

        /// <summary>
        /// Sets and gets the TestReportTitle property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string TestReportTitle
        {
            get
            {
                return _testReportTitle;
            }

            set
            {
                if (_testReportTitle == value)
                {
                    return;
                }

                RaisePropertyChanging(TestReportTitlePropertyName);
                _testReportTitle = value;
                RaisePropertyChanged(TestReportTitlePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="DepartmentName" /> property's name.
        /// </summary>
        public const string DepartmentNamePropertyName = "DepartmentName";

        private string _departmentName = string.Empty;

        /// <summary>
        /// Sets and gets the DepartmentName property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string DepartmentName
        {
            get
            {
                return _departmentName;
            }

            set
            {
                if (_departmentName == value)
                {
                    return;
                }

                RaisePropertyChanging(DepartmentNamePropertyName);
                _departmentName = value;
                RaisePropertyChanged(DepartmentNamePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="TestDateDisplay" /> property's name.
        /// </summary>
        public const string TestDateDisplayPropertyName = "TestDateDisplay";

        private string _testDateDisplay = string.Empty;

        /// <summary>
        /// Sets and gets the TestDateDisplay property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string TestDateDisplay
        {
            get
            {
                return _testDateDisplay;
            }

            set
            {
                if (_testDateDisplay == value)
                {
                    return;
                }

                RaisePropertyChanging(TestDateDisplayPropertyName);
                _testDateDisplay = value;
                RaisePropertyChanged(TestDateDisplayPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="DignosticMessageList" /> property's name.
        /// </summary>
        public const string DignosticMessageListPropertyName = "DignosticMessageList";

        private ObservableCollection<DiagnosticMessageReference> _dignosticMessageList = new ObservableCollection<DiagnosticMessageReference>();

        /// <summary>
        /// Sets and gets the DignosticMessageList property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<DiagnosticMessageReference> DignosticMessageList
        {
            get
            {
                return _dignosticMessageList;
            }

            set
            {
                if (_dignosticMessageList == value)
                {
                    return;
                }

                RaisePropertyChanging(DignosticMessageListPropertyName);
                _dignosticMessageList = value;
                RaisePropertyChanged(DignosticMessageListPropertyName);
            }
        }

        /// <summary>
        /// Load the history data for list and chart.
        /// </summary>
        /// <param name="testItem">Load the history data based on provided test type.</param>
        public override void LoadHistoryData(SingleTestItemViewModel testItem)
        {
            base.LoadHistoryData(testItem);

            // A4 size can fit for 6 ScoliosisATI test items ONLY in history data anaylisys report since it contains Line Trend chart.
            // A4 size can fit 16 other test items in history data anaylisys report.
            int returnedRowCount = testItem is TestItemScoliosisATIViewModel ? 6 : 16;

            var data = ViewModelHelper.LoadTestResultData(ServiceFactory.CreateTestTrackingService(), testItem,
                PatientBasicInfoDisplayViewModel.Number, returnedRowCount);

            HistoryAnalysisChartSource = new ObservableCollection<SingleTestItemViewModel>(data);
        }

        /// <summary>
        /// Set current all properties based on provided instance of BasicConfiguration.
        /// </summary>
        /// <param name="entity">The instance of BasicConfiguration that contains all data.</param>
        private void SetProperties(BasicConfiguration entity)
        {
            CompanyName = entity.CompanyName;
            CompanyLogoFileFullName = entity.CompanyLogoFileFullName;
            CompanyAddress = entity.CompanyAddress;
            CompanyPhoneNumber = entity.CompanyPhoneNumber;
            TestReportTitle = entity.TestReportTitle;
            DepartmentName = entity.DepartmentName;
        }
    }
}
